//
//  ViewController.m
//  FlickrSearching
//
//  Created by HOCVIEN on 4/25/15.
//  Copyright (c) 2015 HOCVIEN. All rights reserved.
//

#import "ViewController.h"
#import "FlickrHelper.h"

@interface ViewController (){
    NSArray *arrTop;
    NSMutableArray *resultArray;
   // BOOL search;
    
    
    
}

@end

@implementation ViewController

- (void)viewDidLoad {
    
    
    
    
    [FlickrHelper getTopicsPlace:^(NSArray *result) {
        arrTop = result;
        [self.tableView reloadData];
        
    }];
    
    
    resultArray = [[NSMutableArray alloc]init];
    
    
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView==self.tableView) {
        return arrTop.count;
    }else{
        return resultArray.count;
        
    }
    
    
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
        NSString *identifier = @"an";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            
        }
    if (indexPath.row%2==0) {
        cell.backgroundColor = [UIColor blueColor];
    }else{
        cell.backgroundColor = [UIColor greenColor];
    }
        if (self.searchbar.text.length==0) {
            NSDictionary *dic = arrTop[indexPath.row];
            
            cell.textLabel.text = dic[@"woe_name"];
            cell.detailTextLabel.text = dic[@"photo_count"];
            
        }else {
            NSDictionary *dic = resultArray[indexPath.row];
            
            cell.textLabel.text = dic[@"woe_name"];
            cell.detailTextLabel.text = dic[@"photo_count"];
            
            
        }
    return cell;
    
        
        
    }
//-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
//    NSIndexPath *index = [self.tableView indexPathForSelectedRow];
//    long row = [index row];
//    if (self.searchbar.text.length==0) {
//        NSDictionary *dictionary = arrTop[row];
//        NSString *placeId = dictionary[@"place_id"];
//        [userDefault setObject:placeId forKey:@"an"];
//        NSString *ten = dictionary[@"woe_name"];
//        [userDefault setObject:ten forKey:@"name"];
//        NSLog(@"%@",ten);
//        
//        
//        
//    }else{
//        NSDictionary *dictionary = resultArray[row];
//        NSString *placeId = dictionary[@"place_id"];
//        [userDefault setObject:placeId forKey:@"an"];
//        NSString *ten = dictionary[@"woe_name"];
//        [userDefault setObject:ten forKey:@"name"];
//        NSLog(@"%@",ten);
//
//        
//    }
//}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    long row = [indexPath row];
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewController *view = [storyBoard instantiateViewControllerWithIdentifier:@"quoc"];
    [self.navigationController pushViewController:view animated:YES];
    if (self.searchbar.text.length==0) {
        NSDictionary *dictionary = arrTop[row];
        NSString *placeId = dictionary[@"place_id"];
        [userDefault setObject:placeId forKey:@"an"];
        NSString *ten = dictionary[@"woe_name"];
        [userDefault setObject:ten forKey:@"name"];
        NSLog(@"%@",ten);
    }else{
        NSDictionary *dictionary = resultArray[row];
        NSString *placeId = dictionary[@"place_id"];
        [userDefault setObject:placeId forKey:@"an"];
        NSString *ten = dictionary[@"woe_name"];
        [userDefault setObject:ten forKey:@"name"];
        NSLog(@"%@",ten);
        
    }
    
    
}


        //
//    }else{
//        NSString *identifier = @"an";
//        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//        if (cell==nil) {
//            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//            
//        }
//        cell.textLabel.text = resultArray[indexPath.row];
//        return cell;
//        
//
//        
//




    



-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    [resultArray removeAllObjects];
    for (int i=0; i<arrTop.count; i++) {
        NSDictionary *dictionary = arrTop[i];
        NSString *nameW = dictionary[@"woe_name"];
        if ([nameW rangeOfString:searchText].location != NSNotFound) {
            [resultArray addObject:dictionary];
            
        }
    }
    
}







@end
